package commonwebgroup.commonwebartifact.EventHandle;

import commonwebgroup.commonwebartifact.Helper.ColorHelper;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class PlayerInteractEventHandle  implements Listener {

    private void playIronTrapdoorSound(Player player) {
        Objects.requireNonNull(player, "player is not allow null!");
        Location playerLocation = player.getLocation();
        player.playSound(playerLocation, Sound.BLOCK_IRON_TRAPDOOR_OPEN, 1.0f, 1.0f);
    }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {

        Objects.requireNonNull(event, "event is not allow null!");

        Player player = event.getPlayer();
        Objects.requireNonNull(player, "player is not allow null!");

        if (!player.getInventory().getItemInMainHand().getType().equals(Material.CACTUS)) {
            return;
        }

        Block clickBlock = event.getClickedBlock();
        if (clickBlock == null) return;

        List<Material> lst = new ArrayList<>();
        lst.add(Material.OBSERVER);//侦测器
        lst.add(Material.PISTON);//活塞
        lst.add(Material.STICKY_PISTON);//粘性活塞
        lst.add(Material.DISPENSER);//发射器
        lst.add(Material.DROPPER);//投掷器

        if(!lst.contains(clickBlock.getType()))return;

        BlockData blockData = clickBlock.getBlockData();
        Objects.requireNonNull(blockData, "blockData is not allow null!");
        if (blockData instanceof Directional directional) {
            BlockFace currentDirection = directional.getFacing();
            Objects.requireNonNull(currentDirection, "currentDirection is not allow null!");
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){

                BlockFace[] directions = {BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST, BlockFace.NORTH};

                int currentDirectionIndex = Arrays.asList(directions).indexOf(currentDirection);
                int nextDirectionIndex = (currentDirectionIndex + 1) % directions.length;
                BlockFace nextDirection = directions[nextDirectionIndex];

                // 如果当前方向不在方向数组中，则将索引设置为 0
                if (currentDirectionIndex == -1) {
                    nextDirection = directions[0];
                }
                directional.setFacing(nextDirection);

            }else if(event.getAction() == Action.LEFT_CLICK_AIR | event.getAction() == Action.LEFT_CLICK_BLOCK){

                if(directional.getFacing()== BlockFace.UP){
                    directional.setFacing(BlockFace.DOWN);
                }else{
                    directional.setFacing(BlockFace.UP);
                }
            }
            playIronTrapdoorSound(player);
            //最终修改
            clickBlock.setBlockData(directional);
            clickBlock.getState().update();

            //让玩家不要打开容器
            if((clickBlock.getType() == Material.DISPENSER || clickBlock.getType()==Material.DROPPER)){
                event.setCancelled(true);
                return;
            }

        }else {
            player.sendMessage(ColorHelper.Error("失败了！！！我也不知道为啥"));
        }

    }
}
